<template>
  <div class="fs30 p20">
    <div class="df jsb item">时间：
      <picker
        mode="date"
        :value="date"
        @change="bindDateChange"
      >
        <view class="picker">
          <input class="tr" type="text" placeholder="请选择时间" disabled name='date_time' v-model="inputValue.date_time">
        </view>
      </picker>
    </div>
    <div class="df jsb item"  @click="showSinglePicker('user_id')">填表人：
      <input class="tr" type="text" disabled v-model="user_id"  placeholder="请选择填表人" >
      <input type="text" name="user_id" disabled hidden v-model="inputValue.user_id">
    </div>
    <div class="df jsb item">审核状态：
      <div class="tr"><m-select typeCode='medical_state' v-model="inputValue.state"></m-select></div>
      <input name="state" hidden type="text" disabled v-model="inputValue.state"/>
    </div>
    <customer-record  :upImg='inputValue.examination_body' @getData='getData'></customer-record>
    <mpvue-picker ref="mpvuePicker" :mode="mode" :pickerValueDefault="pickerValueDefault" :deepLength = "deepLength" @onChange="onChange" @onConfirm="onConfirm" @onCancel="onCancel" :pickerValueArray="pickerValueArray"></mpvue-picker>
  </div>
</template>

<script>
import customerRecord from '@/components/customerRecord'
import uuid from 'uuid/v1'
import mSelect from '@/components/m_select'
import mpvuePicker from 'mpvue-picker'
import { getCusReport, updataeCusReport, addCusReport, getUserList } from '@/api'
export default {
  data () {
    return {
      user_id: '',
      inputValue: {
        // 客户id,
        customer_id: '',
        // 选择填表人id
        user_id: '',
        // 检查结果
        state: '',
        // 时间
        date_time: '',
        examination_body: []
      },
      mode: 'selector',
      pickerValueArray: [],
      pickerValueDefault: [],
      actionsuser_id: [],
      type: ''
    }
  },
  components: {
    customerRecord,
    mpvuePicker,
    mSelect
  },
  methods: {
    bindDateChange (e) {
      this.inputValue.date_time = e.mp.detail.value
    },
    showSinglePicker (type) {
      this.type = type
      this.pickerValueArray = this['actions' + type]
      this.$refs.mpvuePicker.show()
    },
    onConfirm  (e) {
      this.user_id = e.label
      this.inputValue[this.type] = e.value[0]
    },
    async getData (data) {
      let result = ''
      let text = ''
      if (this.$mp.query.id !== 'undefined') {
        this.inputValue.examination_body = data
        text = '修改'
      } else {
        this.inputValue.id = uuid()
        this.inputValue.customer_id = this.$mp.query.cusId
        this.inputValue.examination_body = data
        text = '新增'
      }
      if (!this.inputValue.date_time || !this.inputValue.user_id || !this.inputValue.state || this.inputValue.examination_body.length === 0) {
        wx.showToast({
          title: '请将信息填写完整',
          icon: 'none',
          duration: 2000
        })
      } else {
        if (text === '修改') {
          result = await updataeCusReport(this.inputValue)
        } else {
          result = await addCusReport(this.inputValue)
        }
        if (result.errno === 0) {
          wx.showToast({
            title: text + '成功',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 2000)
        } else {
          wx.showToast({
            title: text + '失败，请重试',
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  },
  async mounted () {
    let result = wx.getStorageSync('useInfo')
    let orgId = result.orgList[0].id
    // 获取用户列表
    let res = await getUserList(orgId)
    this.actionsuser_id = []
    res.data.forEach(element => {
      this.actionsuser_id.push({
        label: element.name,
        value: element.id
      })
    })
    this.inputValue.customer_id = this.$mp.query.cusId
    // 如果是查询状态
    if (this.$mp.query.id !== 'undefined') {
      const result = await getCusReport(this.$mp.query.id)
      this.inputValue = result.data
      for (let i in this.actionsuser_id) {
        if (this.inputValue.user_id === this.actionsuser_id[i].value) {
          this.user_id = this.actionsuser_id[i].label
        }
      }
    } else {
      this.user_id = ''
      this.inputValue = {
        // 客户id,
        customer_id: '',
        // 选择填表人id
        user_id: '',
        // 检查结果
        state: '',
        // 时间
        date_time: '',
        examination_body: []
      }
    }
  }
}
</script>

<style scoped>
.item{padding: 20rpx 0;border-bottom: 2rpx solid #eee }
</style>
